package com.torry.workflow.service.wo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.torry.workflow.api.user.api.UserApi;
import com.torry.workflow.api.user.entity.User;
import com.torry.workflow.common.exception.BizException;
import com.torry.workflow.api.wo.entity.ProcessNode;
import com.torry.workflow.api.wo.entity.ProcessUserGroup;
import com.torry.workflow.api.wo.entity.WoDetail;
import com.torry.workflow.api.wo.entity.WoInfo;
import com.torry.workflow.service.wo.mapper.ProcessUserGroupMapper;
import com.torry.workflow.service.wo.service.IProcessService;
import com.torry.workflow.service.wo.utils.DroolsUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProcessServiceImpl implements IProcessService {
    @Resource
    DroolsUtil droolsUtil;
    @Resource
    private ProcessUserGroupMapper processUserGroupMapper;
    @DubboReference
    private UserApi userApi;

    private boolean checkCondition(WoInfo wo, ProcessNode node) {
        droolsUtil.executeRules(wo, node);
        return node.isEnable();
    }


    @Override
    public List<ProcessNode> getWoRealProcess(WoInfo wo, List<ProcessNode> nodes) {
        return nodes.stream()
                .filter(node -> node.getCondition() == null || checkCondition(wo, node))
                .collect(Collectors.toList());
    }

    @Override
    public List<ProcessNode> revertProcess(WoDetail woDetail) {
        List<ProcessNode> newNodes = new ArrayList<>();
        List<ProcessNode> processNodes = JSON.parseArray(woDetail.getProcess(), ProcessNode.class);
        for (int i = processNodes.size() - 1; i >= 0; i--) {
            newNodes.add(processNodes.get(i));
        }

        return newNodes;
    }

    @Override
    public List<ProcessUserGroup> getAllUserGroups() {
        return processUserGroupMapper.selectList(new QueryWrapper<ProcessUserGroup>().eq("deleted", 0));
    }

    @Override
    public void insertUserGroup(ProcessUserGroup group) {
        ProcessUserGroup userGroup = processUserGroupMapper.selectOne(new QueryWrapper<ProcessUserGroup>().eq("deleted", 0)
                .eq("name", group.getName()));
        if (userGroup != null) throw new BizException(400, "用户组已存在");
        processUserGroupMapper.insert(group);
    }

    @Override
    public void updateUserGroup(ProcessUserGroup group) {
        ProcessUserGroup processUserGroup = processUserGroupMapper.selectOne(new QueryWrapper<ProcessUserGroup>().eq("deleted", 0)
                .eq("name", group.getName()).ne("id", group.getId()));
        if (processUserGroup != null) throw new BizException(400, "用户组已存在");
        processUserGroupMapper.updateById(group);
    }

    @Override
    public void deleteUserGroup(Integer userGroupId) {
        ProcessUserGroup processUserGroup = processUserGroupMapper.selectById(userGroupId);
        processUserGroup.setDeleted(1);
        processUserGroupMapper.updateById(processUserGroup);
    }

    @Override
    public List<User> getUsersByUserGroupId(Integer userGroupId) {
        ProcessUserGroup processUserGroup = processUserGroupMapper.selectById(userGroupId);
        String userIds = processUserGroup.getUserIds();
        if (StringUtils.isBlank(userIds)) return new ArrayList<>();

        List<Integer> userIdList = new ArrayList<>();
        for (String userId : userIds.split(",")) {
            userIdList.add(Integer.parseInt(userId));
        }
        return userApi.getUsersByIds(userIdList);
    }
}
